import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/views/home/Home.vue'

import { getTokenLocal } from "@/utils/auth.js"
import {
	Dialog
} from "vant";
Vue.use(Router)

let router = new Router({
	// mode: 'history',
	base: process.env.BASE_URL,
	routes: [
		//首页
		{
			path: '/',
			name: 'home',
			component: Home,
			meta:{
				title:"首页"
			}
		},

		{ //商品列表
			path: "/product/list",
			name: "product",
			component: () => import( /* webpackChunkName: "product" */ '@/views/product/ProductList.vue'),
			meta:{
				title:"商品列表"
			}
		},
		{ // 商品详情
			path: "/product/detail",
			name: "productDetail",
			component: () => import( /* webpackChunkName: "product" */ '@/views/product/ProductDetail.vue'),
			meta:{
				title:"商品详情"
			}
		},

		//购物车
		{
			path: "/shopcart",
			component: () => import( /* webpackChunkName: "shopcart" */ "@/views/shopcart/Shopcart.vue"),
			name: "shopcart",		
			meta:{
				title:"购物车",
				requireAuth:true,
			}
		},
		
		// 我的
		{
			path: "/user",
			component: () => import("@/views/user/User.vue"),
			name: "user",
			meta:{
				title:"我的"
			}
		},
		
		{
			path: "/login",
			component: () => import("@/views/user/Login.vue"),
			name: "login",
			meta:{
				title:"登录"
			}
		},
		
		{
			path: "/register",
			component: () => import("@/views/user/Register.vue"),
			name: "register",
			meta:{
				title:"注册"
			}
		},
		
		{
			path: "/profile",
			component: () => import("@/views/user/Profile.vue"),
			name: "profile",
			meta:{
				title:"个人信息"
			}
		},
		
		
		
		
		{ //定义订单创建路由
			path: '/order/create',
			name: 'orderCreate',
			component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderCreate.vue'),
			meta: {
				title: "新建订单"
			}
		},
		{ //定义订单列表路由
			path: '/order/list',
			name: 'orderList',
			component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderList.vue'),
			meta: {
				title: "订单列表"
			}
		},
		{ //定义订单明细路由
			path: '/order/detail', 			
			name: 'orderDetail',			
			component: () => import( /* webpackChunkName: "order" */ '@/views/order/OrderDetail.vue'),
			meta: {
				title: "订单详情"
			}
		},
		
		{ //定义地址列表路由
			path: '/address/list',
			name: 'addressList',
			component: () => import( /* webpackChunkName: "address" */ '@/views/address/AddressList.vue'),
			meta: {
				title: "地址列表"
			}
		},
		
		{ //定义地址编辑路由
			path: '/address/edit', 			
			name: 'addressEdit',
			component: () => import( /* webpackChunkName: "address" */ '@/views/address/AddressEdit.vue'),
			meta: {
				title: "地址编辑"
			}
		},
		
		
		
		//工作中提示
		{
			path: "/working",
			component: () => import("@/views/Working.vue"),
			name: "working",
			meta:{
				title:"工作中..."
			}
		},
		
		//不存在路由提示
		//要放在最后
		{ 
			path: "*",
			name: "notFound",
			component: () => import( /* webpackChunkName: "notfound" */ '@/views/NotFound.vue'),
			meta:{
				title:"找不到路由"
			}
		},

	]
})

//设置全局路由导航守卫
router.beforeEach((to, from, next) => {

    if (to.meta.title) {
        document.title = to.meta.title;
    }

    if (to.name === 'login') { // 路由条状到login的，直接放行
        console.log(`${from.name}跳转到login`); // 控制台调试输出
        next(); // 路由继续往下跳转
        return;
    }

    if (!to.meta.requireAuth) { // 路由不需要验证的，直接放行
        console.log(`${from.name}跳转到${to.name},目的路由不需要用户认证`); // 控制台调试输出
        next();
        return;
    }

    // 路由需要验证的
    const token = getTokenLocal();
    if (!token) { // 如果token不存在的，则跳转登录login
        console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token不存在！强制跳转到login`);

        Dialog.confirm({
            message: "没有Token,需要重新登录吗?",
        }).then(() => {
            next({
                name: 'login',
                params: {
                    redirect: to.path
                }
            });
        }).catch(() => {
            router.push({
                name: "home"
            });
        });
        return;
    }

    console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token也存在！`);
    // 其余情况，则路由放行
    next(); //
});

export default router